﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HuynhQuangWebsite.Models;
using THS.HtmlHelpers;
using System.IO;
namespace HuynhQuangWebsite.Controllers
{
    public class ManageBannerController : BaseController
    {
        huynhquangco_websiteEntities ctx = new huynhquangco_websiteEntities();
        //
        // GET: /ManageAbout/
        const int PageSize = 6;
        public ActionResult IndexBanner(int page = 1)
        {
            var model = new HomeAdminListViewModel();
            IList<Banner> banners = ctx.Banners.OrderBy(p=>p.BannerSortOrder).ToList();
            model.BannerList = banners.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.BannerInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = banners.Count() };
            return View(model);
            
        }
        public ActionResult Paging(int page = 1)
        {
            var model = new BannerListViewModel();
            IList<Banner> banners = ctx.Banners.ToList();
            model.BannerList = banners.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.BannerPageInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = banners.Count() };
            return PartialView("UCBannerList",model);

        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdateToAbout(int id, FormCollection frm)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOnDashBoard", "DashBoard");

                var about = (from aboutus in ctx.AboutUs where aboutus.AboutUsID == id select aboutus).SingleOrDefault();
                if (about != null){
                    about.AboutUsTitle = frm["AboutUsTitle"];
                    about.AboutUsDetails = frm["AboutUsDetails"];
                    UpdateModel(about);
                    ctx.SaveChanges();
                    return RedirectToAction("IndexAbout");
                }
                else {
                    about = new AboutU();
                    about.AboutUsTitle = frm["AboutUsTitle"];
                    about.AboutUsDetails = frm["AboutUsDetails"];
                    ctx.AddToAboutUs(about);
                    ctx.SaveChanges();
                    return RedirectToAction("IndexBanner");
                }
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Detail(int id)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var partner = (from partners in ctx.Banners where partners.BannerID == id select partners).SingleOrDefault();
            if (partner != null)
                return View(partner);
            else
            {
                partner = new Models.Banner();
                return View(partner);
            }
            //return trang thông báo lỗi
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Detail(int id, HttpPostedFileBase uploadFile)
        {
            try
            {
                // TODO: Add update logic here
                // if (!IsAuthenticateAdmin())
                //   return RedirectToAction("LogOn", "AdminHome");
                Banner cust = ctx.Banners.Where(p => p.BannerID == id).SingleOrDefault();

                if (uploadFile != null)
                {
                    String oldImage = cust.BannerImg;
                    String oldThumbImage = cust.BannerImgThumb;
                    var fileName = Path.GetFileName(uploadFile.FileName);
                    String sFileName = Utilities.GenerateImageName(Request, Utilities.VirtualPath, fileName, 1.ToString());
                    string filePath = Path.Combine(Request.MapPath(Utilities.VirtualPath), sFileName);
                    String fileThumbPath = Path.Combine(Request.MapPath(Utilities.VirtualPath + "Thumbs/"), sFileName);
                    MakeImagesAndThumbFile(uploadFile.InputStream, filePath, fileThumbPath, 980, 400, 130, 130, 100);
                    cust.BannerImg = Utilities.PhysicalPath + sFileName;
                    cust.BannerImgThumb = String.Format("{0}Thumbs/{1}", Utilities.PhysicalPath, sFileName);
                    //delete image
                    DeleteFile(Request, Utilities.VirtualPath, oldImage);
                    DeleteFile(Request, Utilities.VirtualPath + "Thumbs", oldThumbImage);
                }

                UpdateModel(cust);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã cập nhật!";
                return View(cust);
                //return RedirectToAction("IndexPartner");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add delete logic here
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "AdminHome");
                Banner cust = ctx.Banners.Where(p => p.BannerID == id).SingleOrDefault();
                String oldImage = cust.BannerImg;
                String oldThumbImage = cust.BannerImgThumb;
                ctx.Banners.DeleteObject(cust);
                ctx.SaveChanges();
                DeleteFile(Request, Utilities.VirtualPath, oldImage);
                DeleteFile(Request, Utilities.VirtualPath + "Thumbs", oldThumbImage);
                return RedirectToAction("IndexBanner");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /FunRingManagement/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "BannerID")]Banner cust, HttpPostedFileBase uploadFile)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            try
            {
                if (uploadFile != null)
                {
                    var fileName = Path.GetFileName(uploadFile.FileName);
                    String sFileName = Utilities.GenerateImageName(Request, Utilities.VirtualPath, fileName, 1.ToString());
                    string filePath = Path.Combine(Request.MapPath(Utilities.VirtualPath), sFileName);
                    String fileThumbPath = Path.Combine(Request.MapPath(Utilities.VirtualPath + "Thumbs/"), sFileName);
                    MakeImagesAndThumbFile(uploadFile.InputStream, filePath, fileThumbPath,980, 400, 130, 130, 100);
                    cust.BannerImg = Utilities.PhysicalPath + sFileName;
                    cust.BannerImgThumb = String.Format("{0}Thumbs/{1}", Utilities.PhysicalPath, sFileName);
                }
                ctx.AddToBanners(cust);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã tạo thành công!";
                return View();
            }
            catch
            {
                return View();

            }
        }
    }
}
